//
//  LDBaseAddressPickerView.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/4/28.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit

class LDBaseAddressPickerView: UIView {

    var pickView = UIPickerView()

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.frame = CGRectMake(0, LDFrame.ScreenHeight,LDFrame.ScreenWidth, 0)
        backgroundColor = LDGolbalColor.White
        layer.cornerRadius = 3
        creatview(frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    var originSize:CGSize! = CGSizeZero
<<<<<<< HEAD
    let SampleTag = 777
    
    var marginBottom:CGFloat = 5

    var cancelBtn:UIButton?
    var titleLab:UILabel?
    var enterBtn:UIButton?
    
    var headViewH:CGFloat = 30
    var headTitle:String?
    var lineView:UIView?
    
    var tapClosure:((title:String) -> ())?
    
    lazy var gampView:UIView? = {
=======
    private let SampleTag = 777
    
    private var marginBottom:CGFloat = 5

    private var cancelBtn:UIButton?
    private var titleLab:UILabel?
    private var enterBtn:UIButton?
    
    private var headViewH:CGFloat = 30
    private var headTitle:String?
    private var lineView:UIView?
    
    private var tapClosure:((title:String) -> ())?
    private var sureClosure: (Void->())?
    
    lazy var gampView:UIView? = {
        
>>>>>>> dingdong2.1.0
        let gampview = UIView.init(frame: LDFrame.ScreenBounds)
        gampview.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.4)
        gampview.userInteractionEnabled = true
        gampview.bk_whenTapped({ [weak self] in
            self?.dismissview("")
            })
        return gampview
        
    }()

    func creatview(frame:CGRect) {
        
        cancelBtn = UIButton.setDetailsBtn(nil, picName: "common_back", btnBackColor: LDGolbalColor.White, titleColor: LDFontColor.Red)
        cancelBtn?.titleLabel?.font = LDFont.Font14

        cancelBtn?.bk_whenTapped({ [weak self] in
            self?.dismissview("取消")
        })
        
        
        enterBtn = UIButton.setDetailsBtn(nil, picName: "common_check", btnBackColor: LDGolbalColor.White, titleColor: LDFontColor.Red)
        enterBtn?.titleLabel?.font = LDFont.Font14
        enterBtn?.bk_whenTapped({ [weak self] in
            self?.dismissview("确定")
<<<<<<< HEAD
            })
=======
            
            if self?.sureClosure != nil {
                self?.sureClosure!()
            }
        })
>>>>>>> dingdong2.1.0
        
        titleLab = UILabel.setDetailsLab("城市选择", fontSize: 15, titleColor: LDFontColor.DeepGray, Alignment: .Center)
        
        lineView = UIView()
        lineView?.backgroundColor = LDIconColor.DeepGray
        
        addSubview(cancelBtn!)
        addSubview(enterBtn!)
        addSubview(titleLab!)
        addSubview(lineView!)
        addSubview(pickView)
        
        cancelBtn?.snp_makeConstraints(closure: { (make) in
<<<<<<< HEAD
            make.top.left.equalTo(15)
            make.width.height.equalTo(15)
        })
        enterBtn?.snp_makeConstraints(closure: { (make) in
            make.top.equalTo(15)
            make.right.equalTo(-15)
            make.width.height.equalTo(15)
=======
            make.top.left.equalTo(10)
            make.width.height.equalTo(22)
        })
        enterBtn?.snp_makeConstraints(closure: { (make) in
            make.top.equalTo(10)
            make.right.equalTo(-10)
            make.width.height.equalTo(22)
>>>>>>> dingdong2.1.0
        })
        titleLab?.snp_makeConstraints(closure: { (make) in

            make.centerY.equalTo(cancelBtn!)
            make.left.equalTo((cancelBtn?.snp_right)!)
            make.right.equalTo(enterBtn!.snp_left)
            make.height.equalTo(30)
        })
        lineView?.snp_makeConstraints(closure: { (make) in
            make.top.equalTo((cancelBtn?.snp_bottom)!).offset(15)
            make.left.right.equalTo(0)
            make.height.equalTo(0.5)
        })
        pickView.snp_makeConstraints { (make) in
            make.top.equalTo((lineView?.snp_bottom)!)
            make.left.right.equalTo(0)
        }
    }
<<<<<<< HEAD
    //根据数组个数创建
    func showview(target:UIView) {
        
        target.addSubview(gampView!)
        target.addSubview(self)
        
        UIView.animateWithDuration(0.5) {
            self.frame = CGRectMake(0, LDFrame.ScreenHeight - 320,LDFrame.ScreenWidth, 320)
=======
    
    func show() {
        
        showview(self)
    }
    
    func dissmiss() {
        
        dismissview("取消")
    }
    //根据数组个数创建
    func showview(target:UIView) {
        
//        target.addSubview(gampView!)
//        target.addSubview(self)
        UIApplication.sharedApplication().sendAction(#selector(resignFirstResponder), to: nil, from: nil, forEvent: nil)
        let tar = UIApplication.sharedApplication().keyWindow?.rootViewController?.childViewControllers[0]
        
        tar?.view.frame = LDFrame.ScreenBounds
        tar?.view.addSubview(gampView!)
        tar?.view.addSubview(self)
        
        UIView.animateWithDuration(0.5) {
            
//            if tar?.navigationController?.navigationBar.frame.size.height == 0 {
            
                self.frame = CGRectMake(0, LDFrame.ScreenHeight - 320 + 64,LDFrame.ScreenWidth, 320)
//            } else {
//                
//                self.frame = CGRectMake(0, LDFrame.ScreenHeight - 320,LDFrame.ScreenWidth, 320)
//            }
            
>>>>>>> dingdong2.1.0
        }
    }
    
    func dismissview(status:String) {
        
        UIView.animateWithDuration(0.5, animations: {
            self.frame.origin.y = LDFrame.ScreenHeight
        }) { (finished) in
            
            if self.tapClosure != nil {
                self.tapClosure!(title: status)
            }
            self.gampView?.removeFromSuperview()
            self.removeFromSuperview()
            
        }
    }
    
<<<<<<< HEAD
=======
    //确定
    func sure(closure:()->()) {
        
        sureClosure = closure
    }
    
>>>>>>> dingdong2.1.0
    func sendStatus(colsure:(title:String) -> ()){
        self.tapClosure = colsure
    }
}